home *** CD-ROM | disk | FTP | other *** search
/ PC World Komputer 2010 April / PCWorld0410.iso / hity wydania / Ubuntu 9.10 PL / karmelkowy-koliberek-desktop-9.10-i386-PL.iso / casper / filesystem.squashfs / usr / bin / u1sdtool < prev    next >
Text File  |  2009-10-29  |  6KB  |  161 lines

  1. #! /usr/bin/env python
  2.  
  3. # u1sdtool - command line utility for controlling ubuntuone-syncdaemon
  4. #
  5. # Author: Guillermo Gonzalez <guillermo.gonzalez@canonical.com>
  6. #
  7. # Copyright 2009 Canonical Ltd.
  8. #
  9. # This program is free software: you can redistribute it and/or modify it
  10. # under the terms of the GNU General Public License version 3, as published
  11. # by the Free Software Foundation.
  12. #
  13. # This program is distributed in the hope that it will be useful, but
  14. # WITHOUT ANY WARRANTY; without even the implied warranties of
  15. # MERCHANTABILITY, SATISFACTORY QUALITY, or FITNESS FOR A PARTICULAR
  16. # PURPOSE.  See the GNU General Public License for more details.
  17. #
  18. # You should have received a copy of the GNU General Public License along
  19. # with this program.  If not, see <http://www.gnu.org/licenses/>.
  20. from twisted.internet import glib2reactor
  21. glib2reactor.install()
  22.  
  23. import dbus
  24. import codecs
  25. import os
  26. import sys
  27. import warnings
  28.  
  29. from dbus.mainloop.glib import DBusGMainLoop
  30. from optparse import OptionParser
  31. from twisted.internet import reactor
  32.  
  33. from ubuntuone.syncdaemon.tools import SyncDaemonTool
  34. from ubuntuone.syncdaemon.dbus_interface import DBUS_IFACE_NAME
  35. from ubuntuone.syncdaemon.tools import (
  36.     show_path_info, 
  37.     show_uploads, 
  38.     show_downloads, 
  39.     show_shares,
  40.     show_shared,
  41. )
  42.  
  43.  
  44. def main(argv, stdout):
  45.     usage = "Usage: %prog [option]"
  46.     parser = OptionParser(usage=usage)
  47.     parser.add_option("-w", "--wait", dest="wait", action="store_true",
  48.                       help="Wait until ubuntuone-syncdaemon reachs nirvana")
  49.     parser.add_option("", "--accept-share", dest="accept_share",
  50.                       metavar="SHARE_ID",
  51.                       help="accept the share with the specified id")
  52.     parser.add_option("", "--reject-share", dest="reject_share",
  53.                       metavar="SHARE_ID",
  54.                       help="reject the share with the specified id")
  55.     parser.add_option("", "--list-shares", dest="list_shares",
  56.                       action="store_true",
  57.                       help=" get the list of shares")
  58.     parser.add_option("", "--refresh-shares", dest="refresh_shares",
  59.                       action="store_true",
  60.                       help=" request a refresh of the list of shares to"
  61.                       " the server")
  62.     parser.add_option("", "--offer-share", dest="offer_share",
  63.                       metavar="PATH USER SHARE_NAME ACCESS_LEVEL",
  64.                       help=" share PATH to USER. ")
  65.     parser.add_option("", "--list-shared", dest="list_shared",
  66.                       action="store_true",
  67.                       help=" list the shared path's/shares offered. ")
  68.     parser.add_option("", "--refresh", dest="refresh_path",
  69.                       metavar="PATH", help=" request a refresh of PATH")
  70.     parser.add_option("", "--info", dest="path_info",
  71.                       metavar="PATH", help=" request the metadata of PATH")
  72.     parser.add_option("", "--current-transfers", dest="current_transfers",
  73.                       action="store_true",
  74.                       help=" show the current uploads and downloads")
  75.     parser.add_option("-q", "--quit", dest="quit", action='store_true',
  76.                       help="shutdown the syncdaemon")
  77.  
  78.     (options, args) = parser.parse_args(argv)
  79.  
  80.     loop = DBusGMainLoop(set_as_default=True)
  81.     bus = dbus.SessionBus(mainloop=loop)
  82.     sync_daemon_tool = SyncDaemonTool(bus)
  83.  
  84.     # get the encoding of the output stream, defaults to UTF-8
  85.     out_encoding = getattr(stdout, 'encoding', 'utf-8')
  86.     if out_encoding is None:
  87.         out_encoding = 'utf-8'
  88.     out = codecs.getwriter(out_encoding)(stdout, errors='replace')
  89.     if options.wait:
  90.         def callback(result):
  91.             """ wait_for_nirvana callback (stop the reactor and exit)"""
  92.             out.write("\nubuntuone-syncdaemon became a fully "
  93.                       "enlightened Buddha!\n")
  94.  
  95.         d = sync_daemon_tool.wait_for_nirvana(verbose=True)
  96.         d.addCallbacks(callback)
  97.     elif options.list_shares:
  98.         d = sync_daemon_tool.get_shares()
  99.         d.addCallback(lambda r: show_shares(r, out))
  100.     elif options.accept_share:
  101.         d = sync_daemon_tool.accept_share(options.accept_share)
  102.     elif options.reject_share:
  103.         d = sync_daemon_tool.reject_share(options.reject_share)
  104.     elif options.refresh_shares:
  105.         d = sync_daemon_tool.refresh_shares()
  106.     elif options.offer_share:
  107.         if len(args) != 4:
  108.             parser.error('--offer-share requires 4 arguments')
  109.         d = sync_daemon_tool.offer_share(options.offer_share, *args[1:])
  110.     elif options.list_shared:
  111.         d = sync_daemon_tool.list_shared()
  112.         d.addCallback(lambda r: show_shared(r, out))
  113.     elif options.refresh_path:
  114.         if not os.path.exists(options.refresh_path):
  115.             parser.error("PATH: '%s' don't exists" % \
  116.                          options.refresh_path)
  117.         d = sync_daemon_tool.query_by_path(options.refresh_path)
  118.     elif options.path_info:
  119.         if not os.path.exists(options.path_info):
  120.             parser.error("PATH: '%s' don't exists" % \
  121.                          options.path_info)
  122.         d = sync_daemon_tool.get_metadata(options.path_info)
  123.         d.addCallback(lambda r: show_path_info(r, options.path_info, out))
  124.     elif options.current_transfers:
  125.         d = sync_daemon_tool.get_current_uploads()
  126.         d.addCallback(lambda r: show_uploads(r, out))
  127.         d.addCallback(lambda _: sync_daemon_tool.get_current_downloads())
  128.         d.addCallback(lambda r: show_downloads(r, out))
  129.     elif options.quit:
  130.         d = sync_daemon_tool.quit()
  131.         def shutdown_check(result):
  132.             if result is None and \
  133.                DBUS_IFACE_NAME in bus.list_names():
  134.                 out.write("ubuntuone-syncdaemon stopped.\n")
  135.             else:
  136.                 out.write("ubuntuone-syncdaemon still running.\n")
  137.         d.addBoth(shutdown_check)
  138.     else:
  139.         parser.print_help()
  140.         sys.exit(1)
  141.  
  142.     def default_errback(error):
  143.         """ default error handler. """
  144.         out.write("\nOops, an error ocurred:\n")
  145.         error.printTraceback()
  146.  
  147.     def stop_reactor(result):
  148.         """ stop the reactor. """
  149.         if reactor.running:
  150.             reactor.stop()
  151.     d.addErrback(default_errback)
  152.     d.addCallback(stop_reactor)
  153.     reactor.run()
  154.  
  155.  
  156. if __name__ == '__main__':
  157.     # disable the dbus warnings
  158.     warnings.filterwarnings('ignore', module='dbus')
  159.     main(sys.argv, sys.stdout)
  160.  
  161.